package dev.entao.travel.admin

import dev.entao.base.Label
import dev.entao.core.Action
import dev.entao.core.HttpContext
import dev.entao.core.render.sendBack
import dev.entao.core.render.sendResult
import dev.entao.html.div
import dev.entao.html.linkButton
import dev.entao.page.*
import dev.entao.page.bs.tableActions
import dev.entao.sql.AND
import dev.entao.sql.ConstraintException
import dev.entao.sql.EQ
import dev.entao.sql.update
import dev.entao.travel.AdminPages
import dev.entao.travel.model.User

//@IconPair("class", "bi-person")
//@SideGroup
@NavGroup(icon = "static/user.png")
@Label("用户管理")
class UserPages(context: HttpContext) : AdminPages(context) {

    @SortParams
    @PageParams
    @NavItem(order = 0, label = "用户", icon = "static/query.png")
    @Label("查询")
    @Action
    fun list() {
        val thisAction = ::list
        val w = equalProps(User::id, User::phone, User::status) AND User::nickName.LIKE_IN()

        val rowCount = User.count(w)
        val itemList = User.list(w) {
            orderPage(thisAction)
            limitPage(thisAction)
        }
        dashboard {
            workTitle("用户查询")
            workToolbar {
                linkButton("btn", "btn-sm", "btn-outline-primary") {
                    fromAction(::addDialog)
                }
            }
            workspace {
                div("col-12") {
                    queryForm(::list) {
                        queryDiv {
                            labelEdit(User::id)
                        }
                        queryDiv {
                            labelEdit(User::nickName)
                        }
                        queryDiv {
                            labelEdit(User::phone)
                        }

                        queryDiv {
                            labelSelect(User::status)
                        }
                    }

                    tableActions(::enable, ::disable, this@UserPages::delete)
                    tableX(itemList) {
                        itemIDProp(User::id)
                        sortAction(::list)
                        columnCheck()
                        columnProp(User::id).sortable()
                        columnProp(User::nickName).sortable()
                        columnProp(User::phone).sortable()
                        columnProp(User::status).sortable()
                        columnDate(User::regDate).sortable()
                        columnDate(User::regTime).sortable()
                        columnProp(User::portrait).sortable()
                        columnAction(::edit, ::del)
                    }

                    tablePaginationX(::list, rowCount)
                }
            }
        }
    }

    @ConfirmMessage("要删除这些记录吗?")
    @Label("删除")
    @Action
    fun delete(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            User.deleteByKey(n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("启用")
    @Action
    fun enable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            User.update(User::status to 0, User::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("冻结")
    @Action
    fun disable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            User.update(User::status to 1, User::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @ConfirmMessage("要删除这条记录吗?")
    @Label("删除")
    @Action
    fun del(id: Int) {
        User.deleteByKey(id)
        context.sendBack {
            removeKey("id")
            setSuccess("已删除:ID=$id 的记录")
        }
    }

    @Action
    fun addJson() {
        try {
            val a = User()
            a.fromRequest()
            a.status = 0
            a.insert()
            context.sendResult {
                success()
            }
        } catch (ex: ConstraintException) {
            context.sendResult { failed("手机号已存在") }
        }
    }

    @DialogPop
    @Label("添加")
    @Action
    fun addDialog() {
        sendDialog {
            dialogTitle { +"添加用户" }
            dialogForm(::addJson) {
                div {
                    labelEdit(User::nickName)
                }
                div {
                    labelEdit(User::phone)
                }
                div {
                    labelEdit(User::pwd)
                }

            }
        }
    }

    @DialogPop
    @Label("编辑")
    @Action
    fun edit(id: Int) {
        val row = User.oneByKey(id) ?: return
        sendDialog {
            dialogTitle { +"编辑商品类别" }
            dialogForm(::save) {
                div {
                    labelEdit(row::id) {
                        readonly()
                    }
                }
                div {
                    labelEdit(row::nickName)
                }

                div {
                    labelEdit(row::phone)
                }
                div {
                    labelEdit(row::pwd)
                }


                div {
                    labelEdit(row::identNum)
                }

                div {
                    labelSelect(row::status)
                }

                div {
                    inputFileRef(row::portrait)
                }
            }
        }
    }

    @Label("保存")
    @Action
    fun save(id: Int) {
        try {
            val a = User.oneByKey(id) ?: return
            a.update {
                it.fromRequest()
            }
            context.sendResult {
                success()
            }
        } catch (ex: ConstraintException) {
            context.sendResult { failed("错误：" + ex.localizedMessage) }
        }
    }
}